/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package packForm.Cadastros;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import packDatabase.DaoAcess;
import packForm.Padroes.FrmPesquisar;

/**
 *
 * @author everaldo
 */
public class FrmCadMesas extends javax.swing.JInternalFrame {
    private DaoAcess conexao = new DaoAcess();
    /**
     * Creates new form frmCadMesas
     */
    public FrmCadMesas() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        txtCodigo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtNomeEmrpresa = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCodEmpresa = new javax.swing.JTextField();
        btnPesquisarEmpresa = new javax.swing.JButton();
        spnSituacao = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnSalvar = new javax.swing.JButton();
        btnNovo = new javax.swing.JButton();
        btnProcurar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cadastro de Mesas");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtCodigo.setEditable(false);

        jLabel1.setText("Código:");

        txtNomeEmrpresa.setEditable(false);

        jLabel2.setText("Empresa:");

        txtCodEmpresa.setEditable(false);

        btnPesquisarEmpresa.setText("...");
        btnPesquisarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarEmpresaActionPerformed(evt);
            }
        });

        spnSituacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Livre", "Ocupado" }));

        jLabel3.setText("Situação:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtCodEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPesquisarEmpresa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNomeEmrpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(spnSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNomeEmrpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisarEmpresa)
                    .addComponent(spnSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(387, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnProcurar.setText("Procurar");
        btnProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcurarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProcurar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 575, Short.MAX_VALUE)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnNovo)
                    .addComponent(btnProcurar)
                    .addComponent(btnExcluir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        int statusMesa=0;
        if(spnSituacao.getSelectedItem().toString().equals("Livre")){
            statusMesa=0;
        }else if(spnSituacao.getSelectedItem().toString().equals("Ocupado")){
            statusMesa=1;
        }
        conexao.OpenConect();
        if(txtCodigo.getText().equals("")){//insert
            if(!txtCodEmpresa.getText().equals("")){
                conexao.Insert("INSERT INTO TAB_MESAS(STATUS,CODEMPRESA) VALUES("+statusMesa+","+txtCodEmpresa.getText()+")");
                limpaTela();
                JOptionPane.showMessageDialog(null, "Mesa cadastrada com sucesso!", "Atenção",1);             
            }else{
                JOptionPane.showMessageDialog(null, "Informe a empresa que a mesa pertence!", "Atenção",0);                             
            }
        }else{//update
             if(!txtCodEmpresa.getText().equals("")){
                 conexao.Update("UPDATE TAB_MESAS SET STATUS="+statusMesa+",CODEMPRESA="+txtCodEmpresa.getText()+" WHERE CODMESA="+txtCodigo.getText());
                 limpaTela();
                 JOptionPane.showMessageDialog(null, "Mesa atualizada com sucesso!", "Atenção",1);                             
             }else{
                JOptionPane.showMessageDialog(null, "Informe a empresa que a mesa pertence!", "Atenção",0);                             
            }
        }
        conexao.CloseConect();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnPesquisarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarEmpresaActionPerformed
        FrmPesquisar pesquisa = new FrmPesquisar(null,true,"CODEMPRESA AS 'Código', RAZAOSOCIAL AS 'Razão Social', CNPJ AS 'CNPJ'","TAB_EMPRESAS","","");
        pesquisa.comboPesquisa.addItem("CodEmpresa");
        pesquisa.comboPesquisa.addItem("RazaoSocial");
        pesquisa.comboPesquisa.addItem("CNPJ");
        pesquisa.setVisible(true);
        if(pesquisa.dados.size()!=0){
            txtCodEmpresa.setText(pesquisa.dados.get(0));
            txtNomeEmrpresa.setText(pesquisa.dados.get(1));
        }
    }//GEN-LAST:event_btnPesquisarEmpresaActionPerformed

    private void btnProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcurarActionPerformed
        FrmPesquisar pesquisa = new FrmPesquisar(null,true,"CODMESA AS 'Código', STATUS AS 'Status', CODEMPRESA AS 'Código da Empresa'","TAB_MESAS","","");
        pesquisa.comboPesquisa.addItem("CodMesa");
        pesquisa.comboPesquisa.addItem("Status");
        pesquisa.comboPesquisa.addItem("CodEmpresa");
        pesquisa.setVisible(true);
        if(pesquisa.dados.size()!=0){
            try{
                conexao.OpenConect();
                conexao.Select("SELECT RAZAOSOCIAL FROM TAB_EMPRESAS WHERE CODEMPRESA="+pesquisa.dados.get(2));
                conexao.rs.first();
                txtCodigo.setText(pesquisa.dados.get(0));
                txtCodEmpresa.setText(pesquisa.dados.get(2));
                txtNomeEmrpresa.setText(conexao.rs.getString("RAZAOSOCIAL"));
                conexao.CloseConect();
            }catch(SQLException e){
                System.out.print("erro: "+e.getMessage());
            } 
        }
    }//GEN-LAST:event_btnProcurarActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        limpaTela();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if (!txtCodigo.getText().equals("")){
            conexao.OpenConect();
            conexao.Delete("DELETE FROM TAB_MESAS WHERE CODMESA="+txtCodigo.getText());
            limpaTela();
            conexao.CloseConect();
            JOptionPane.showMessageDialog(null, "Dados Exluidos com sucesso!", "Atenção",1);                                         
        }else{
            JOptionPane.showMessageDialog(null, "Primeiramente selecione uma mesa!", "Atenção",0);                             
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    public void limpaTela(){
        txtCodigo.setText("");
        txtCodEmpresa.setText("");
        txtNomeEmrpresa.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnPesquisarEmpresa;
    private javax.swing.JButton btnProcurar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox spnSituacao;
    private javax.swing.JTextField txtCodEmpresa;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNomeEmrpresa;
    // End of variables declaration//GEN-END:variables
}
